package com.cloud.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.utils.DictUtils;
import com.cloud.common.enums.CheckType;
import com.cloud.exception.CustomException;
import com.cloud.system.entity.SysDictData;
import com.cloud.system.entity.SysDictType;
import com.cloud.system.mapper.SysDictDataMapper;
import com.cloud.system.mapper.SysDictTypeMapper;
import com.cloud.system.service.ISysDictTypeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典 业务层处理
 *
 * @author zx, yzj
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    @Resource
    private SysDictTypeMapper dictTypeMapper;

    @Resource
    private SysDictDataMapper dictDataMapper;

    // 项目启动时，初始化字典到缓存
//    @PostConstruct
//    public void init() {
//        List<SysDictType> dictTypeList = dictTypeMapper.selectDictTypeAll();
//        for (SysDictType dictType : dictTypeList) {
//            List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType(dictType.getDictType());
//            DictUtils.setDictCache(dictType.getDictType(), dictDataList);
//        }
//    }

    /**
     * 根据条件分页查询字典类型
     *
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeList(SysDictType dictType) {
        return dictTypeMapper.selectDictTypeList(dictType);
    }

    /**
     * 根据所有字典类型
     *
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeAll() {
        return dictTypeMapper.selectDictTypeAll();
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDataList = DictUtils.getDictCache(dictType);
        if (ObjectUtil.isNotEmpty(dictDataList)) {
            return dictDataList;
        }
        dictDataList = dictDataMapper.selectDictDataByType(dictType);
        if (ObjectUtil.isNotEmpty(dictDataList)) {
            DictUtils.setDictCache(dictType, dictDataList);
            return dictDataList;
        }
        return null;
    }

    /**
     * 根据字典类型ID查询信息
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public SysDictType selectDictTypeById(Long dictId) {
        return dictTypeMapper.selectDictTypeById(dictId);
    }

    /**
     * 根据字典类型查询信息
     *
     * @param dictType 字典类型
     * @return 字典类型
     */
    @Override
    public SysDictType selectDictTypeByType(String dictType) {
        return dictTypeMapper.selectDictTypeByType(dictType);
    }

    /**
     * 批量删除字典类型信息
     *
     * @param dictIds 需要删除的字典ID
     * @return 结果
     */
    @Override
    public int deleteDictTypeByIds(List<Long> dictIds) {
        for (Long dictId : dictIds) {
            SysDictType dictType = selectDictTypeById(dictId);
            if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0) {
                throw new CustomException(dictType.getDictType() + "字典类型已分配,不能删除");
            }
        }
        int rows = dictTypeMapper.deleteDictTypeByIds(dictIds);
        if (rows > 0) {
            DictUtils.clearDictCache();
        }
        return rows;
    }

    /**
     * 清空缓存数据
     */
    @Override
    public void clearCache() {
        // 清除字典缓存
        DictUtils.clearDictCache();
        SysDictData dictData = new SysDictData();
        dictData.setStatus(0);
        Map<String, List<SysDictData>> dictDataMap = dictDataMapper.selectDictDataList(dictData)
                .stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        // 重新字典缓存
        for (Map.Entry<String, List<SysDictData>> entry : dictDataMap.entrySet()) {
            DictUtils.setDictCache(entry.getKey(), entry.getValue().stream()
                    .sorted(Comparator.comparing(SysDictData::getDictSort))
                    .collect(Collectors.toList()));
        }
    }

    /**
     * 新增保存字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    public int insertDictType(SysDictType dictType) {
        return dictTypeMapper.insert(dictType);
    }

    /**
     * 修改保存字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDictType(SysDictType dictType) {
        SysDictType oldDict = dictTypeMapper.selectDictTypeById(dictType.getDictId());
        dictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType());
        int rows = dictTypeMapper.updateById(dictType);
        if (rows > 0) {
            DictUtils.deleteDictData(oldDict.getDictType());
        }
        return rows;
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param dict 字典类型
     * @return 结果
     */
    @Override
    public String checkDictTypeUnique(SysDictType dict) {
        int rows = dictTypeMapper.checkDictTypeUnique(dict.getDictType(), dict.getDictId());
        if (rows > 0) {
            return CheckType.NOT_UNIQUE.getCode();
        }
        return CheckType.UNIQUE.getCode();
    }

}
